---
# title: "Width"
title: "宽度"
# description: "Utilities for setting the width of an element"
description: "用来设置元素宽度的功能类"
features:
  responsive: true
  customizable: true
  hover: false
  focus: false
---

import plugin from 'tailwindcss/lib/plugins/width'
import { Variants } from '@/components/Variants'
import { Disabling } from '@/components/Disabling'
import { numbersFirst } from '@/utils/sortClasses'

export const classes = {
  plugin,
  sort: numbersFirst,
}

## Auto

<!-- Use `w-auto` to let the browser calculate and select the width for the element. You can use it to unset a specific width: -->
使用 `w-auto` 让浏览器计算并选择元素的宽度。您可以用它来取消设置的一个特定的宽度。

```html
<div class="w-24 **md:w-auto** ..."></div>
```

## Screen Width

<!-- Use `w-screen` to make an element span the entire width of the viewport. -->
使用 `w-screen` 使一个元素跨越整个视口的宽度。

```html
<div class=" h-12 **w-screen**"></div>
```

## Fixed Width

<!-- Use `w-{number}` or `w-px` to set an element to a fixed width. -->
使用 `w-{number}` 或 `w-px` 将元素设置为固定宽度。


```html lightBlue
<template preview>
  <div class="flex flex-col space-y-8 mx-auto w-64">
    <div class="flex items-center space-x-4">
      <p class="text-sm text-light-blue-600 w-12 text-right">w-8</p>
      <div class="h-8 w-8 rounded-md bg-light-blue-500 text-white text-2xl font-extrabold"></div>
    </div>
    <div class="flex items-center space-x-4">
      <p class="text-sm text-light-blue-600 w-12 text-right">w-12</p>
      <div class="h-8 w-12 rounded-md bg-light-blue-500 text-white text-2xl font-extrabold"></div>
    </div>
    <div class="flex items-center space-x-4">
      <p class="text-sm text-light-blue-600 w-12 text-right">w-16</p>
      <div class="h-8 w-16 rounded-md bg-light-blue-500 text-white text-2xl font-extrabold"></div>
    </div>
    <div class="flex items-center space-x-4">
      <p class="text-sm text-light-blue-600 w-12 text-right">w-24</p>
      <div class="h-8 w-24 rounded-md bg-light-blue-500 text-white text-2xl font-extrabold"></div>
    </div>
  </div>
</template>

<div>
  <div class="**w-8** ..."></div>
  <div class="**w-12** ..."></div>
  <div class="**w-16** ..."></div>
  <div class="**w-24** ..."></div>
</div>
```

## Fluid Width

<!-- Use `w-{fraction}` or `w-full` to set an element to a percentage based width. -->
使用 `w-{fraction}` 或 `w-full` 将元素设置为基于百分比的宽度。

```html rose
<template preview>
  <div class="space-y-6">
    <div class="flex rounded-md overflow-hidden text-white font-mono">
      <div class="w-1/2 px-2 py-4 bg-rose-400 text-center">w-1/2</div>
      <div class="w-1/2 px-2 py-4 bg-rose-500 text-center">w-1/2</div>
    </div>
    <div class="flex rounded-md overflow-hidden text-white font-mono">
      <div class="w-2/5 px-2 py-4 bg-rose-400 text-center">w-2/5</div>
      <div class="w-3/5 px-2 py-4 bg-rose-500 text-center">w-3/5</div>
    </div>
    <div class="flex rounded-md overflow-hidden text-white font-mono">
      <div class="w-1/3 px-2 py-4 bg-rose-400 text-center">w-1/3</div>
      <div class="w-2/3 px-2 py-4 bg-rose-500 text-center">w-2/3</div>
    </div>
    <div class="flex rounded-md overflow-hidden text-white font-mono">
      <div class="w-1/4 px-2 py-4 bg-rose-400 text-center">w-1/4</div>
      <div class="w-3/4 px-2 py-4 bg-rose-500 text-center">w-3/4</div>
    </div>
    <div class="flex rounded-md overflow-hidden text-white font-mono">
      <div class="w-1/5 px-2 py-4 bg-rose-400 text-center">w-1/5</div>
      <div class="w-4/5 px-2 py-4 bg-rose-500 text-center">w-4/5</div>
    </div>
    <div class="flex rounded-md overflow-hidden text-white font-mono">
      <div class="w-1/6 px-2 py-4 bg-rose-400 text-center">w-1/6</div>
      <div class="w-5/6 px-2 py-4 bg-rose-500 text-center">w-5/6</div>
    </div>
    <div class="rounded-md w-full px-2 py-4 bg-rose-500 text-center text-white font-mono">w-full</div>
  </div>
</template>


<div class="flex ...">
  <div class="**w-1/2** ... ">w-1/2</div>
  <div class="**w-1/2** ..."">w-1/2</div>
</div>
<div class="flex ...">
  <div class="**w-2/5** ...">w-2/5</div>
  <div class="**w-3/5** ...">w-3/5</div>
</div>
<div class="flex ...">
  <div class="**w-1/3** ...">w-1/3</div>
  <div class="**w-2/3** ...">w-2/3</div>
</div>
<div class="flex ...">
  <div class="**w-1/4** ...">w-1/4</div>
  <div class="**w-3/4** ...">w-3/4</div>
</div>
<div class="flex ...">
  <div class="**w-1/5** ...">w-1/5</div>
  <div class="**w-4/5** ...">w-4/5</div>
</div>
<div class="flex ...">
  <div class="**w-1/6** ...">w-1/6</div>
  <div class="**w-5/6** ...">w-5/6</div>
</div>
<div class="**w-full** ...">w-full</div>
```

<!-- ## Responsive -->
## 响应式

<!-- To control the width of an element at a specific breakpoint, add a `{screen}:` prefix to any existing width utility. For example, adding the class `md:w-full` to an element would apply the `w-full` utility at medium screen sizes and above. -->
要在特定的断点处控制元素的宽度，可以在任何现有的宽度功能类前添加 `{screen}: `前缀。例如，将 `md:w-full` 类添加到一个元素中，就可以在中等及以上尺寸屏幕的情况下应用用 `w-full` 功能类。

```html
<div class="w-1/2 **md:w-full** ...">
  <!-- ... -->
</div>
```

<!-- For more information about Tailwind's responsive design features, check out the [Responsive Design](/docs/responsive-design) documentation. -->
关于 Tailwind 的响应式设计功能的更多信息，请查看 [响应式设计](/docs/responsive-design) 文档。

<!-- ## Customizing -->
## 定制

### Width scale

<!-- By default, Tailwind's width scale is a combination of the [default spacing scale](/docs/customizing-spacing#default-spacing-scale) as well as some additional values specific to widths. -->
默认情况下，Tailwind 的宽度比例是 [默认间距比例](/docs/customizing-spacing#default-spacing-scale) 以及一些特定于宽度的附加值的组合。

<!-- You can customize the spacing scale for padding, margin, width, and height all at once in the `theme.spacing` or `theme.extend.spacing` sections of your `tailwind.config.js` file: -->
您可以在您的 `tailwind.config.js` 文件中的 `theme.spacing` 或 `theme.extend.spacing` 部分一次性自定义 padding、margin、width 和 height 的间距比例。

```diff-js
  // tailwind.config.js
  module.exports = {
    theme: {
      extend: {
+       spacing: {
+         '72': '18rem',
+         '84': '21rem',
+         '96': '24rem',
+       }
      }
    }
  }
```

<!-- To customize width separately, use the `theme.width` section of your `tailwind.config.js` file. -->
要单独定制宽度，请使用您的 `tailwind.config.js` 文件中的 `theme.width` 部分。

```diff-js
  // tailwind.config.js
  module.exports = {
    theme: {
      extend: {
+       width: {
+         '1/7': '14.2857143%',
+         '2/7': '28.5714286%',
+         '3/7': '42.8571429%',
+         '4/7': '57.1428571%',
+         '5/7': '71.4285714%',
+         '6/7': '85.7142857%',
+       }
      }
    }
  }
```

<!-- Learn more about customizing the default theme in the [theme customization documentation](/docs/theme#customizing-the-default-theme). -->
在 [主题自定义文档](/docs/theme#customizing-the-default-theme) 中了解更多关于自定义默认主题的信息。

### 变体

<Variants plugin="width" />

### 禁用

<Disabling plugin="width" />
